import { createFilterColumns, createTableRequestParams } from '@/utils/table';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { Button } from 'antd';
import { useMemo, useRef, useState } from 'react';
import api from './request';

function InventoryLog() {
  const actionRef = useRef<ActionType>();

  const [filterList, setFilterList] = useState<Requestor.FilterResponse[]>([]);

  // request
  const getFilterList = async () => {
    // const filterList = await api.getFilterList();
    const filterList: Requestor.FilterResponse[] = [];
    setFilterList(filterList);
    return filterList;
  };
  const getListRequest = (
    params: Requestor.TableParams,
    filterList: Requestor.FilterResponse[],
  ) => {
    const requestParams = createTableRequestParams(params, filterList);
    return api.getList({ ...requestParams, sortField: 'operationTime' });
  };
  const getList = async (params: Requestor.TableParams) => {
    if (!filterList || !filterList.length) {
      const filterList = await getFilterList();
      return getListRequest(params, filterList);
    } else {
      return getListRequest(params, filterList);
    }
  };

  const columns = useMemo(() => {
    const filterColumns = createFilterColumns<InventoryLogApi.ListResponse>(filterList);
    const tableColumns: ProColumns<InventoryLogApi.ListResponse>[] = [
      {
        title: '商品名称',
        dataIndex: 'goodsName',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '规格名称',
        dataIndex: 'skuName',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '操作类型',
        dataIndex: 'operationTypeName',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '数量',
        dataIndex: 'quantity',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '新数量',
        dataIndex: 'newQuantity',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '旧数量',
        dataIndex: 'oldQuantity',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '操作人',
        dataIndex: 'operatorName',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '操作时间',
        dataIndex: 'operationTime',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '备注',
        dataIndex: 'remarks',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
    ];
    return filterColumns.concat(tableColumns);
  }, [filterList]);
  return (
    <PageContainer>
      <ProTable
        bordered
        size="small"
        rowKey="id"
        options={false}
        actionRef={actionRef}
        pagination={{ defaultPageSize: 10, showSizeChanger: true }}
        toolBarRender={() => []}
        search={{
          optionRender: () => {
            return [
              <Button key="search" type="primary" onClick={() => actionRef.current?.reload()}>
                查询
              </Button>,
            ];
          },
        }}
        columns={columns}
        request={getList}
      />
    </PageContainer>
  );
}

export default InventoryLog;
